var dy=new Vue({
    el:"#dy",
    data:{
        vid:0,
        venue:{},
        dynamics:[],
        imageFile:"",
        content:""
    },
    mounted(){
        axios.get("/venue/session").then(res=>{
            console.info("sessionVenue:")
            console.info(res.data.data);
            this.vid=res.data.data.vid;
            console.info("vid:");
            console.info(this.vid)
            axios.get("/venue/find/"+this.vid).then(e=>{
                console.info(e.data.data);
                this.venue=e.data.data;
                // venue=e.data.data;
                console.info(this.venue)
                this.showDynamics();

            }).catch(er=>{
                console.info(er)
            });
        }).catch(err=>{
            console.info(err)
        });
    },
    methods:{
        post:function () {
            console.info("发布朋友圈")
            axios({
                method:"post",
                url:"/dynamic/add",
                data:{
                    "sender":this.venue.vid,
                    "content":this.content,
                    "avatar":this.venue.avatar,
                    "nickname":this.venue.nickname
                }
            }).then(res=>{
                console.info(res)
                if (res.data.code===200){
                    console.info("我在上传图片前面")
                    //Promise
                    this.uploadImage().then(result=>{
                        console.info("result:")
                        console.info(result)
                        if (result.data.code===200){
                            console.info("我在展示动态前面")
                            this.showDynamics();
                            console.info("我在展示动态后面")
                            alert("发送动态成功！")
                        }
                    });
                    console.info("我在上传图片后面")
                }
            }).catch(err=>{
                console.info(err)
            })

        },
        uploadImage:function () {
            //上传展示图片1
            let formData1 = new FormData();
            formData1.append('type',this.imageFile.type);
            formData1.append('name',this.imageFile.name);
            formData1.append('data',this.imageFile);
            return axios({
                method: 'post',
                url: "/dynamic/image",
                data: formData1,
                headers: {
                    'Content-Type': 'multipart/form-data'
                }
            });
            //     .then(res => {
            //     console.info(res)
            //     this.image1Flag=res.data.code===200;
            //     if (res.data.code===300){
            //         this.accessCode++;
            //     }
            // })
            //     .catch(err => {
            //     });
        },
        showImage:function (e) {
            if (e.target.value===""){
                e.target.nextElementSibling.src="images\\default.png";
                this.imageFile="";
                return;
            }
            this.imageReaderAndShow(e)
            this.imageFile=e.target.files[0];

        },
        imageReaderAndShow:function (e) {
            let fileReader = new FileReader();
            fileReader.readAsDataURL(e.target.files[0]);
            fileReader.onload=function(){
                e.target.nextElementSibling.src=fileReader.result;
            }
        },
        showDynamics:function() {
            //获取该场馆的朋友圈动态
            axios.get("/dynamic/allOfVenue/"+this.vid).then(re=>{
                console.info(re)
                this.dynamics=re.data.data;
                console.info("我在展示动态里面")
            }).catch(error=>{
                console.info(error)
            })
        }

    }
});